import {defineStore} from "pinia"
import {ref, computed} from 'vue'
import { useUserStore } from "@/stores/user.js"
import { insertCartAPI, findNewCartListAPI, delCartAPI, changeCartAPI, changeCartCountAPI } from '@/api/cart.js'

export const useCartStore = defineStore("cart", () => {
    const userStore = useUserStore()
    const isLogin = computed(() => userStore.userInfo.token)

    const cartList = ref([])

    // 获取最新购物车列表
    const updateNewCart = async () => {
        const res = await findNewCartListAPI()
        cartList.value = res.data.result
    }

    const addCart = async (goods) => {
        if(isLogin.value){
            // 登录之后加入购物车逻辑
            const { skuId, count } = goods
            await insertCartAPI({ skuId, count })
            await updateNewCart()
        } else {
            // 添加购物车操作
            const item = cartList.value.find(item => item.skuId === goods.skuId)
            if (item) {
                item.count++
            } else {
                cartList.value.push(goods)
            }
        }
    }

    // 删除购物车
    const delCart = async (skuId) => {
        if(isLogin.value) {
            // 调用接口实现接口购物车中的删除功能
            await delCartAPI([skuId])
            await updateNewCart()
        } else {
            // 找到要删除项的下标值 - splice
            // const index = cartList.value.findIndex(item => item.skuId === skuId)
            // cartList.value.splice(index, 1)
            // 使用数组的过滤方法 - filter
            cartList.value = cartList.value.filter(item => item.skuId !== skuId)
        }
    }

    // 清除购物车
    const clearCart = () => {
        cartList.value = []
    }

    // 单选功能
    const singleCheck = async (skuId, selected) => {
        if(isLogin.value) {
            // 调用接口实现接口购物车中的全选/取消
            await changeCartAPI(selected, [skuId])
            await updateNewCart()
        } else {
            const item = cartList.value.find(item => item.skuId === skuId)
            item.selected = selected
        }
    }

    // 全选功能
    const allCheck = async (selected) => {
        if(isLogin.value) {
            // 调用接口实现接口购物车中的全选/取消
            const ids = cartList.value.map(item => item.skuId)
            await changeCartAPI(selected, ids)
            await updateNewCart()
        } else {
            cartList.value.forEach(item => item.selected = selected)
        }
    }

    // 修改购物车商品数量
    const changeCartCount = async (skuId, count) => {
        if (isLogin.value) {
            await changeCartCountAPI(skuId, count)
            await updateNewCart()
        }
        const item = cartList.value.find(item => item.skuId === skuId)
        item.count = count
    }

    // 总的数量
    const allCount = computed(() => {
        return cartList.value.reduce((sum, item) => sum + item.count, 0)
    })

    // 总的价格
    const allPrice = computed(() => {
        return cartList.value.reduce((sum, item) => sum + item.count * item.price, 0)
    })

    // 已选择的数量
    const selectedCount = computed(() => {
        return cartList.value.filter(item => item.selected).reduce((sum, item) => sum + item.count, 0)
    })

    // 已选择商品价格合计
    const selectedPrice = computed(() => {
        return cartList.value.filter(item => item.selected).reduce((sum, item) => sum + item.count * item.price, 0)
    })

    // 是否全选
    const isAll = computed(() => {
        return cartList.value.every(item => item.selected)
    })

    return {
        cartList,
        addCart,
        delCart,
        allCount,
        allPrice,
        singleCheck,
        isAll,
        allCheck,
        selectedCount,
        selectedPrice,
        updateNewCart,
        clearCart,
        changeCartCount
    }
}, {
    persist: true
})